import { LoginFormState } from "@pages/login";
import apiService from "@service/index";
import { saveCredentials } from "@slice/credentials";

const authApiService = apiService
// 登录
.injectEndpoints({
  endpoints: (builder) => ({
    login: builder.mutation<LoginResponse, LoginFormState>({
      query: (body) => ({
        url: "/authorizations",
        method: "post",
        body,
      }),
      onQueryStarted: async (arg, api) =>{
        // 捕获错误
        try {
          // 等待请求完成
          const response = await api.queryFulfilled;// 请求结束
          // 将用户凭证存储到状态切片
          api.dispatch(saveCredentials(response.data.data));
          console.log(response)
        } catch (error) {}
      },
    }),
  }),
})
// 获取验证码
.injectEndpoints({
  endpoints: (builder) => ({
    sendMsgCode: builder.query<GeekResponse<null>, string>({
      query: (mobile) => ({ url: `/sms/codes/${mobile}` }),
    }),
  }),
});



// 导出登录请求钩子 hooks 函数
export const { useLoginMutation,useLazySendMsgCodeQuery } = authApiService;



